package com.lingxu.module.redList.controller;

import com.lingxu.base.common.api.vo.Result;
import com.lingxu.base.common.constant.LogType;
import com.lingxu.base.common.util.EasyExcelUtils;
import com.lingxu.base.config.LogInfoHandler;
import com.lingxu.base.system.manage.vo.LoginUser;
import com.lingxu.module.redList.entity.RedListTemp;
import com.lingxu.module.redList.entity.TempApproval;
import com.lingxu.module.redList.service.RedListTempService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Title: RedListTempController
 * @Package: com.lingxu.module.redList.controller
 * @Author: ysj闫世杰
 * @Email: 1105324813@qq.com
 * @Date: Created in 2023/12/4 14:39
 * @Description:
 * @Version: 1.0.0
 */
@RestController
@RequestMapping(value = "/redList/temp")
public class RedListTempController {

    @Autowired
    private RedListTempService redListTempService;

    /**
     * @Author ysj闫世杰
     * @Description 新增红名单比对申请
     * @Date 2023/12/4 14:42
     * @Param:
     * @return com.lingxu.base.common.api.vo.Result
     **/
    @RequestMapping(value = "/addRedListTemp")
    public Result addRedListTemp(@RequestBody RedListTemp redListTemp){
        //唯一性校验
        int row = redListTempService.findOnly(redListTemp.getIdcard());
        if(row > 0){
            return Result.error("该身份证号码存在未审批记录，请勿重复提交!");
        }
        LoginUser user =  (LoginUser) SecurityUtils.getSubject().getPrincipal();
        redListTemp.setCreateUser(user.getId());
        redListTemp.setCreateUserName(user.getName());
        redListTemp.setCreateTime(new Date());
        redListTemp.setStatus("0");  //已申请
        redListTemp.setIsRed("-1");
        redListTempService.save(redListTemp);

        //保存日志
        String content = "在【"+ new SimpleDateFormat("yyyy-MM-dd").format(new Date()) +"】新增红名单比对申请。";
        new LogInfoHandler().setLogInfoHandler(LogType.LOG_RED_LIST_CHECK,content);
        return Result.ok("操作成功");
    }

    /**
     * @Author ysj闫世杰
     * @Description 导入Excel
     * @Date 2023/12/4 14:49
     * @Param: file
     * @return com.lingxu.base.common.api.vo.Result
     **/
    @RequestMapping(value = "/importExcelFile")
    public Result importExcelFile(MultipartFile file){
        LoginUser user =  (LoginUser) SecurityUtils.getSubject().getPrincipal();
        List<RedListTemp> redLists = EasyExcelUtils.importExcel(file, 0, 1, RedListTemp.class);
        Date date = new Date();
        redLists.forEach(
            item -> {
                item.setCreateUserName(user.getName());
                item.setCreateUser(user.getId());
                item.setCreateTime(date);
                item.setStatus("0");
                item.setIsRed("-1");
            }
        );
        redListTempService.saveBatch(redLists);
        String content = "在【"+ new SimpleDateFormat("yyyy-MM-dd").format(new Date()) +"】通过Excel导入红名单比对申请。";
        new LogInfoHandler().setLogInfoHandler(LogType.LOG_RED_LIST_CHECK,content);
        return Result.ok("操作成功");
    }

    /**
     * @Author ysj闫世杰
     * @Description 查询列表
     * @Date 2023/12/4 15:02
     * @Param: page
     * @Param: limit
     * @Param: status
     * @return com.lingxu.base.common.api.vo.Result
     **/
    @RequestMapping(value = "/findRedListTemp")
    public Result findRedListTemp(Integer page,Integer limit,String status){
        return redListTempService.findRedListTemp(page,limit,status);
    }


    /**
     * @Author ysj闫世杰
     * @Description 批量审批
     * @Date 2023/12/6 9:27
     * @Param: approval
     * @return com.lingxu.base.common.api.vo.Result
     **/
    @RequestMapping(value = "/updateApprovalBatch")
    public Result updateApprovalBatch(@RequestBody TempApproval approval){
        return redListTempService.updateApprovalBatch(approval);
    }
}
